package com.wl4g.sample.callgo.sample5;

import static java.lang.String.format;
import static java.lang.String.valueOf;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;

import com.wl4g.sample.callgo.utils.JNAGoType.GoString.ByValue;

/**
 * {@link App5}
 * 
 * @author Wangl.sir &lt;wanglsir@gmail.com, 983708408@qq.com&gt;
 * @version 2020年6月21日 v1.0.0
 * @see https://java-native-access.github.io/jna/4.2.1/overview-summary.html
 * @see https://github.com/senx/warp10-ext-jnago
 */
public class App5 {

	// static String json1 =
	// "{\"safeclound_largestDemand\":\"{\"activeDemand\":\"9360.0\",\"totalDemand\":\"9360.0\",\"meterType\":\"134\",\"dataType\":\"ELE_DE_M\",\"bid\":\"QQBE\",\"addr\":\"11111279\",\"reactiveDemand\":\"240.0\",\"cid\":\"YQDYR\",\"order\":\"02\",\"timestamp\":\"1595822599184\"}\"}";
	static String json1 = "abcdefg";

	static ByValue topic = new ByValue("safeclound_largestDemand");
	static ByValue topic2 = new ByValue("safeclound_largestDemand2");

	public static void main(String[] args) throws Exception {
		new Thread(() -> {
			try {
				simpleProducerTest1(5);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();

		new Thread(() -> {
			try {
				simpleConsumerTest2(20);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();
	}

	static void simpleProducerTest1(int count) throws Exception {
		FastmqProducer producer = FastmqProducer.INSTANCE;
		long begin = currentTimeMillis();
		for (int i = 0; i < count; i++) {
			String offerResult = producer.Offer(topic, new ByValue(i + " - " + json1));
			producer.CommitProducer(topic);
			System.out.println(offerResult);
		}
		out.println(format("Producer running cost: %sms", valueOf(currentTimeMillis() - begin)));
	}

	static void simpleConsumerTest2(int count) throws Exception {
		FastmqConsumer consumer = FastmqConsumer.INSTANCE;
		long begin = currentTimeMillis();
		for (int i = 0; i < count; i++) {
			String pollResult = consumer.Poll(topic, 3000L);
			consumer.CommitConsumer(topic);
			// String pollResult = producer.Poll(topic2, 3000L);
			// producer.Commit(topic2);
			System.out.println(pollResult);
		}
		out.println(format("Consumer running cost: %sms", valueOf(currentTimeMillis() - begin)));
	}

}